package org.jadestudio.home.service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.jadestudio.home.dao.ArticleDao;;

public class ArticleService {
	private ArticleDao articleDao; 
	
	public ArticleDao getArticleDao() {
		return articleDao;
	}

	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	
	/**
	 * 获取首页文章列表
	 * @param 
	 */
	public List getHomePageArticleList(){
		try {
			List<java.util.Map> articleList = articleDao.getHomePageArticleList();
			return articleList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/*
	 * 获取文章类型列表
	 */
	public List<java.util.Map> getArticleTypeList(){
		try {
			List<java.util.Map> articleTypeList = articleDao.getArticleTypeList();
			java.util.Map allTypeMap = new java.util.HashMap<Object, Object>();
			allTypeMap.put("article_type_id", "");
			allTypeMap.put("article_type_name", "全部");
			articleTypeList.add(0, allTypeMap);
			return articleTypeList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * 获取游戏筛选列表（含分页）
	 */
	public List<java.util.Map> getArticleList(String category,int pageindex,int pagesize){
		try {
			List<java.util.Map> articleList = articleDao.getArticleList(category, pageindex, pagesize);
			return articleList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * 获取文章筛选列表总页数
	 */
	public int getArticlePageCount(String category,int pagesize){
		try {
			int articlePageCount = articleDao.getArticlePageCount(category, pagesize);
			return articlePageCount;
			
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/*
	 * 获取指定搜索关键字文章（前12个）
	 */
	public List<java.util.Map> searchArticlesByKeyname(String keyname){
		try {
			List<java.util.Map> articleList = articleDao.searchArticlesByKeyname(keyname);
			return articleList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * 获取文章详情BY ID
	 */
	public java.util.Map getArticleDetail(String id) throws SQLException{
		try {
			java.util.Map articleDetail = articleDao.getArticleDetail(id);
			return articleDetail;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	/*
	 * 获取文章相关游戏BY ID
	 */
	public List<java.util.Map> getArticleGameList(String id) throws SQLException{
		try {
			List<java.util.Map> articleGameList = articleDao.getArticleGameList(id);
			return articleGameList;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * 获得文章投票情况BY ID
	 */
	public java.util.Map getArticleVotes(String id,String ip) throws SQLException{
		try {
			List<java.util.Map> articleVoteList = articleDao.getArticleVotes(id,ip);
			java.util.Map result = new java.util.HashMap();
			for(int i=0;i<articleVoteList.size();i++){
				result.put(articleVoteList.get(i).get("article_score_value").toString(), articleVoteList.get(i).get("count"));
			}
			return result;
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	/*
	 * 文章投票
	 */
	public int voteArticle(String article_id, int score, String ip) throws SQLException{
		try {
			return articleDao.voteArticle(article_id, score, ip);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	
	/*
	 * 获取相关文章列表
	 */
	public List<java.util.Map> getSimilarArticleList(String id) throws SQLException{
		try {
			return articleDao.getSimilarArticleList(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}	
}
